# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os
import shutil
import subprocess

from conan import ConanFile
from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, copy

required_conan_version = ">=2.13.0"


class QemuDriversConan(ConanFile):
    name = "qemu_drivers"
    description = "some necessary drivers for qemu"
    topics = ("conan", "tools", "qemu_drivers")
    license = "GPL-2.0-or-later"
    homepage = "https://www.kernel.org/"
    url = "https://github.com/conan-io/conan-center-index"
    settings = "os", "arch", "compiler", "build_type"
    exports_sources = ["permissions.ini", "Makefile", "mock/*"]

    options = {
        "module_symvers": ["ANY"]
    }
    default_options = {
        "module_symvers": "",
    }

    def configure(self):
        pass
    
    def config_options(self):
        module_symvers_path = os.path.join(self.buildenv.vars(self).get("CONAN_CMAKE_FIND_SDK_ROOT"), "Module.symvers")
        self.options.module_symvers = subprocess.check_output(["sha256sum", module_symvers_path], text=True).split()[0]
        print(f"module_symvers_path: {module_symvers_path}, self.options.module_symvers: {self.options.module_symvers}")

    def export_sources(self):
        export_conandata_patches(self)

    def source(self):
        get(self, **self.conan_data["sources"][self.version])
        copy(self, "net/9p/*", src="linux-5.10.1", dst=".")
        copy(self, "fs/9p/*", src="linux-5.10.1", dst=".")
        copy(self, "net/core/*", src="linux-5.10.1", dst=".")
        copy(self, "drivers/dma/*", src="linux-5.10.1", dst=".")
        copy(self, "drivers/block/*", src="linux-5.10.1", dst=".")
        copy(self, "drivers/virtio/*", src="linux-5.10.1", dst=".")
        copy(self, "drivers/net/*", src="linux-5.10.1", dst=".")

    def build(self):
        apply_conandata_patches(self)
        if self.settings.arch == "x86_64":
            return
        CONAN_CMAKE_FIND_SDK_ROOT = self.buildenv.vars(self).get("CONAN_CMAKE_FIND_SDK_ROOT")
        make_command = f"make KERNEL_PATH={self.buildenv.vars(self).get("KERNEL_PATH")} KBUILD_EXTRA_SYMBOLS={CONAN_CMAKE_FIND_SDK_ROOT}/Module.symvers CROSS_COMPILE={self.buildenv.vars(self).get("CHOST")}-"
        self.run(f"{make_command}")

    def package(self):
        if self.settings.arch == "x86_64":
            return
        copy(self,
            "**/*.ko", src=".", dst=os.path.join(self.package_folder, 'lib/modules/qemu/'), keep_path=False)
        copy(self, "permissions.ini", src=".", dst=self.package_folder)
